package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.itheima.reggie.common.BaseContext;
import com.itheima.reggie.common.R;
import com.itheima.reggie.entity.ShoppingCart;
import com.itheima.reggie.service.ShoppingCartService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

@RequestMapping("/shoppingCart")
@RestController
@Slf4j
public class ShoppingCartController {
    @Autowired
    private ShoppingCartService shoppingCartService;
    @PostMapping("/add")
    public R<ShoppingCart>add(@RequestBody ShoppingCart shoppingCart) {
        log.info("购物车数据为", shoppingCart);
        //设置用户id,指定当前使哪个用户的购物车数据
        Long currentId = BaseContext.getCurrentId();
        shoppingCart.setUserId(currentId);
        Long dishId = shoppingCart.getDishId();
        LambdaQueryWrapper<ShoppingCart> lqw = new LambdaQueryWrapper<>();
        lqw.eq(ShoppingCart::getUserId, currentId);
        if (dishId != null) {
            //添加到购物车的是菜品
            lqw.eq(ShoppingCart::getDishId, dishId);
        } else {
            //添加到里面的是套餐
            lqw.eq(ShoppingCart::getSetmealId, shoppingCart.getSetmealId());
        }
        //查询当前菜品或者套餐是否在购物车中
        //sql：select*from shopping_cart where user_id=? and dish_id/setmeal_id=?
        ShoppingCart one = shoppingCartService.getOne(lqw);
        if (one != null) {
            //如果已经存在，就在他原来的数量基础上+1
            Integer number = one.getNumber();
            one.setNumber(number + 1);
            shoppingCartService.updateById(one);
        } else {
            //如果不存在，则添加到购物车，数量默认就是1
            shoppingCart.setNumber(1);
            shoppingCart.setCreateTime(LocalDateTime.now());
            shoppingCartService.save(shoppingCart);
            one = shoppingCart;
        }
        return R.success(one);
    }
        //查询购物车
        @GetMapping("/list")
        public R<List<ShoppingCart>>list(ShoppingCart shoppingCart){
            LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(ShoppingCart::getUserId,BaseContext.getCurrentId());
            queryWrapper.orderByAsc(ShoppingCart::getCreateTime);
            List<ShoppingCart> list1 = shoppingCartService.list(queryWrapper);
            return  R.success(list1);
        }
        //删除购物车
         @DeleteMapping("/clean")
             public R<String>clean(){
        //SQL:delete from shopping_cart where user_id=?
             LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();
             queryWrapper.eq(ShoppingCart::getUserId,BaseContext.getCurrentId());
             shoppingCartService.remove(queryWrapper);
             return  R.success("清空购物车成功");
         }
    @PostMapping("/sub")
    public R<ShoppingCart>sub(@RequestBody ShoppingCart shoppingCart) {
        log.info("购物车数据为", shoppingCart);
        //设置用户id,指定哪个用户的购物车数据
        Long currentId = BaseContext.getCurrentId();
        //用当前的用户id设置 shoppingCart
        shoppingCart.setUserId(currentId);
        //取得dishid
        Long dishId = shoppingCart.getDishId();
        LambdaQueryWrapper<ShoppingCart> lqw= new LambdaQueryWrapper<>();
        lqw.eq(ShoppingCart::getUserId,currentId);
        if(dishId !=null){
            //添加到购物车的都是菜品
            lqw.eq(ShoppingCart::getDishId,dishId);
        }else{
            lqw.eq(ShoppingCart::getSetmealId,shoppingCart.getSetmealId());
        }
            //查询当前菜品是否在购物车中
        ShoppingCart one = shoppingCartService.getOne(lqw);
        if(one!=null){
            //如果已经存在，就在他原来基础上-1
            Integer number = one.getNumber();
            if (one.getNumber()>0) {
                one.setNumber(number - 1);
            }
            shoppingCartService.updateById(one);

        }else{
            shoppingCart.setNumber(1);
            shoppingCart.setCreateTime(LocalDateTime.now());
             shoppingCartService.save(shoppingCart);

        }
    return  R.success(one);


//        //设置用户id,指定当前使哪个用户的购物车数据
//        Long currentId = BaseContext.getCurrentId();
//        shoppingCart.setUserId(currentId);
//        Long dishId = shoppingCart.getDishId();
//        LambdaQueryWrapper<ShoppingCart> lqw = new LambdaQueryWrapper<>();
//        lqw.eq(ShoppingCart::getUserId, currentId);
//        if (dishId != null) {
//            //添加到购物车的是菜品
//            lqw.eq(ShoppingCart::getDishId, dishId);
//        } else {
//            //添加到里面的是套餐
//            lqw.eq(ShoppingCart::getSetmealId, shoppingCart.getSetmealId());
//        }
//        //查询当前菜品或者套餐是否在购物车中
//        //sql：select*from shopping_cart where user_id=? and dish_id/setmeal_id=?
//        ShoppingCart one = shoppingCartService.getOne(lqw);
//        if (one != null) {
//            //如果已经存在，就在他原来的数量基础上+1
//            Integer number = one.getNumber();
//            one.setNumber(number + 1);
//            shoppingCartService.updateById(one);
//        } else {
//            //如果不存在，则添加到购物车，数量默认就是1
//            shoppingCart.setNumber(1);
//            shoppingCart.setCreateTime(LocalDateTime.now());
//            shoppingCartService.save(shoppingCart);
//            one = shoppingCart;
//        }
//        return R.success(one);
    }


    }

